package com.homework.phase1.module4;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;

public class RunSystem  {

    public void run() {
        InfoManagement infoManagement = new InfoManagement();
        System.out.println("欢迎进入学生信息管理系统");


        Scanner sc = new Scanner(System.in);


        boolean flag = true;
        while (flag) {
            System.out.println("请输入以下任意功能指令：增加、修改、删除、查找、遍历、退出");
            String command = sc.next();
            switch (command) {

                case "增加":
                    infoManagement.addStudent();break;

                case "修改":
                    infoManagement.modifyStudent();break;

                case "删除":
                    infoManagement.removeStudent(); break;

                case "查找":
                    infoManagement.findStudent(); break;

                case "遍历":
                    System.out.println(infoManagement.allStudent()); break;

                case "退出":
                    flag = false;
                    //System.out.println(infoManagement.list);

                    //通过file类，对存放student数据的文件进行判断，如果之前存在，就删除，如果没有，就新建一个
                    File f = new File("G:/hadoop/student.txt");

                    if (f.exists()) {
                        f.delete();
                    } else {
                        try {
                            f.createNewFile();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    //创建缓冲字符输出流，对list数组进行遍历，按student对象逐行存入文件内
                    BufferedWriter bw = null;

                    try {
                         bw = new BufferedWriter(new FileWriter("G:/hadoop/student.txt"));
                        for (Object l:infoManagement.list) {
                            bw.write(l.toString());
                            bw.newLine();
                            //System.out.println(l);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        if (null != bw){

                            try {
                                bw.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                        }
                    }

                    System.out.println("谢谢使用，下次再见！");
                    break;
            }
        }
    }

}
